package com.heima.wemedia.controller;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dto.WmMaterialDto;
import com.heima.model.wemedia.entity.WmMaterial;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 自媒体素材表(WmMaterial)表控制层
 *
 * @author makejava
 * @since 2022-04-10 16:22:32
 */
@RestController
@RequestMapping("/api/v1/material")
public class WmMaterialController {

    /**
     * 服务对象
     */
    @Resource
    private WmMaterialService wmMaterialService;

    //素材上传
    @PostMapping("/upload_picture")
    public ResponseResult upload(MultipartFile multipartFile) throws IOException {
        return wmMaterialService.upload(multipartFile);
    }


    //分页查询
    @PostMapping("/list")
    public ResponseResult list(@RequestBody WmMaterialDto dto) {
        return wmMaterialService.list(dto);
    }

    /**
     * 删除图片
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/del_picture/{id}")
    public ResponseResult deleteById(@PathVariable Long id) {
        return wmMaterialService.deleteById(id);
    }

    //更改为收藏
    @GetMapping("/collect/{id}")
    public ResponseResult update(@PathVariable Integer id) {
        return wmMaterialService.updateById(id);
    }

    //取消收藏
    @GetMapping("/cancel_collect/{id}")
    public ResponseResult updates(@PathVariable Integer id) {
        return wmMaterialService.updatesByIds(id);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<WmMaterial> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.wmMaterialService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param wmMaterial 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<WmMaterial> add(WmMaterial wmMaterial) {
        return ResponseEntity.ok(this.wmMaterialService.insert(wmMaterial));
    }

    /**
     * 编辑数据
     *
     * @param wmMaterial 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<WmMaterial> edit(WmMaterial wmMaterial) {
        return ResponseEntity.ok(this.wmMaterialService.update(wmMaterial));
    }


}

